# ============================================================================
# 
# yptools.pm
#
# account maint NIS tools module
#
# $Id: yptools.pm,v 1.4 2005/08/25 22:38:27 dyoung2 Exp $
#
# ============================================================================
#
# ChangeLog:
#
# $Log: yptools.pm,v $
# Revision 1.4  2005/08/25 22:38:27  dyoung2
#   * Standardized BEGIN headers.
#
# Revision 1.3  2005/08/25 18:15:41  dyoung2
#   * removed ! perl header
#
# Revision 1.2  2005/04/26 22:30:19  dyoung2
# update
#
# Revision 1.1  2005/04/08 23:15:57  dyoung2
#   * Initial version.
#
# ============================================================================

my  $cvsid   = '$Id: yptools.pm,v 1.4 2005/08/25 22:38:27 dyoung2 Exp $';
my  @cvsinfo = split( ' ', $cvsid );
our $NAME    = File::Basename::basename($0);
our $VERSION = $cvsinfo[2];

# ------------------------------------------------------------------------------
#                             B E G I N
# ------------------------------------------------------------------------------
BEGIN {

    # Pragmas
    use strict;

    # "Standard" modules we use
    use FindBin;
    use Config::Simple;
    use lib "$FindBin::Bin/../lib";

    # Standard account maint modules to use
    use logging;
    use errcodes;
    use utils;
    
    # Modules that this module uses

}


# ------------------------------------------------------------------------------ 
#                             V A R I A B L E S
# ------------------------------------------------------------------------------ 
our $LOGFILE    = "$FindBin::Bin/../log/acctmgr.log" unless($LOGFILE); 
our $CONFIGFILE = "$FindBin::Bin/../etc/acctmgr.cfg" unless($CONFIGFILE); 


# ------------------------------------------------------------------------------
#                             C O N F I G   F I L E
# ------------------------------------------------------------------------------
our $CFG = new Config::Simple( filename => $CONFIGFILE ) or die "$!\n" unless($CFG);


# ==============================================================================
# NAME        : 
# DESCRIPTION : 
# ARGUMENTS   : 
# RETURN      : 
# NOTES       : 
# ==============================================================================
sub _ypmake {
    my ($map) = @_;
    my $name = "_ypmake";
    my ( $package, $filename, $line ) = caller;
    debug("$name: entering with args @_");
    debug("$name: called from package->$package, filename->$filename, line->$line");


    # Make sure they give us a yp map to build
    unless ($map) {
        logmsg("$name: ERROR, you have to give me a map to build!");
        return(0);
    }

    # make sure the global var for the ypdir to perform the make in
    if ( ! defined($YPDIR) ) {
        logmsg("$name: global var YPDIR isn't set (check the config file)");
        return(0);
    }

    # make sure the make program is defined and is present
    if ( defined($MAKEPGM) ) {
        if ( ! -x $MAKEPGM ) { 
            logmsg("$name: make program $MAKEPGM is present but not executable");
            return(0);
        }
    } else {
        logmsg("$name: FAILED no make program defined in global MAKEPGM");
        return(0);
    }

    my $cmd   = "cd $YPDIR && $MAKEPGM $map";
    my $retval = 1;

    logmsg("$name: building nis $map map in $YPDIR with $MAKEPGM");

    if ( open( YPMAKE, "$cmd |") ) {
        logmsg("$name: opened make command as pipe");
    } else {
        logmsg("$name: FAILED to open make command as pipe");
    }
 
    while(<YPMAKE>) {
        chomp($_);
        if ( $_ =~ /updated $map/ ) {
            logmsg("$name: updated $map map");
        } elsif ( $_ =~ /pushed $map/ ) {
            logmsg("$name: pushed $map map");
        } elsif ( $_ =~ /Nothing to be done/ ) {
            logmsg("$name: nothing to build in $YPDIR");
        } else {
            logmsg("$name: FAILED, unknown output received: $_");
            $retval = 0;
        }
    }

    close( YPMAKE );
    debug("$name: returning with val $retval");
    return($retval);
}

1;
